﻿var db = require('../utils/db');

var rooms = {};
var creatingRooms = {};

var userLocation = {};
var totalRooms = 0;

var REN_SHU = [2, 4];
var WAN_FA = [1];
var PIAO_CAI = [1, 2];
var CAI_SHEN = [1, 2];
var FENG_DING = [50, 60, 80];
var JU_SHU = [8, 16];

function generateRoomId() {
	var roomId = "";
	for (var i = 0; i < 6; ++i) {
		roomId += Math.floor(Math.random() * 10);
	}
	return roomId;
}

function constructRoomFromDb(dbdata) {
	var conf = JSON.parse(dbdata.base_info);
	var roomInfo = {
		uuid: dbdata.uuid,
		id: dbdata.id,
		numOfGames: dbdata.num_of_turns,
		createTime: dbdata.create_time,
		nextButton: dbdata.next_button,
		seats: new Array(conf.renshu),
		conf: conf
	};


	roomInfo.gameMgr = require("./gamemgr_xlch");
	var roomId = roomInfo.id;

	for (var i = 0; i < conf.renshu; ++i) {
		var s = roomInfo.seats[i] = {};
		s.userId = dbdata["user_id" + i];
		s.score = dbdata["user_score" + i];
		s.name = dbdata["user_name" + i];
		s.ready = false;
		s.seatIndex = i;
		s.numOfGang = 0;
		s.numOfPeng = 0;

		if (s.userId > 0) {
			userLocation[s.userId] = {
				roomId: roomId,
				seatIndex: i
			};
		}
	}

	rooms[roomId] = roomInfo;
	totalRooms++;
	return roomInfo;
}

exports.createRoom = function (creator, roomConf, ip, port, callback) {
	if (
		roomConf.renshu == null
		|| roomConf.wanfa == null
		|| roomConf.piaocai == null
		|| roomConf.caishen == null
		|| roomConf.fengding == null
		|| roomConf.jushu == null) {
		callback(1, null);
		return;
	}

	if (roomConf.renshu < 0 || roomConf.renshu > REN_SHU.length) {
		callback(1, null);
		return;
	}

	if (roomConf.wanfa < 0 || roomConf.wanfa > WAN_FA.length) {
		callback(1, null);
		return;
	}

	if (roomConf.piaocai < 0 || roomConf.piaocai > PIAO_CAI.length) {
		callback(1, null);
		return;
	}

	if (roomConf.caishen < 0 || roomConf.caishen > CAI_SHEN.length) {
		callback(1, null);
		return;
	}

	if (roomConf.fengding < 0 || roomConf.fengding > FENG_DING.length) {
		callback(1, null);
		return;
	}

	if (roomConf.jushu < 0 || roomConf.jushu > JU_SHU.length) {
		callback(1, null);
		return;
	}

	var fnCreate = function () {
		var roomId = generateRoomId();
		if (rooms[roomId] != null || creatingRooms[roomId] != null) {
			fnCreate();
		}
		else {
			creatingRooms[roomId] = true;
			db.is_room_exist(roomId, function (ret) {
				if (ret) {
					delete creatingRooms[roomId];
					fnCreate();
				}
				else {
					var createTime = Math.ceil(Date.now() / 1000);
					var roomInfo = {
						uuid: "",
						id: roomId,
						numOfGames: 0,
						createTime: createTime,
						nextButton: 0,
						seats: [],
						conf: {
							renshu: REN_SHU[roomConf.renshu],
							wanfa: WAN_FA[roomConf.wanfa],
							piaocai: PIAO_CAI[roomConf.piaocai],
							caishen: CAI_SHEN[roomConf.caishen],
							fengding: FENG_DING[roomConf.fengding],
							jushu: JU_SHU[roomConf.jushu],
							creator: creator,
						}
					};

					roomInfo.gameMgr = require("./gamemgr_xlch");

					for (var i = 0; i < roomInfo.conf.renshu; ++i) {
						roomInfo.seats.push({
							userId: 0,
							score: 0,
							name: "",
							ready: false,
							seatIndex: i,
							numOfGang: 0,
							numOfPeng: 0
						});
					}

					//写入数据库
					var conf = roomInfo.conf;
					db.create_room(roomInfo.id, conf, ip, port, createTime, function (uuid) {
						delete creatingRooms[roomId];
						if (uuid != null) {
							roomInfo.uuid = uuid;
							rooms[roomId] = roomInfo;
							totalRooms++;
							callback(0, roomId);
						}
						else {
							callback(3, null);
						}
					});
				}
			});
		}
	};

	fnCreate();
};

exports.destroy = function (roomId) {
	var roomInfo = rooms[roomId];
	if (roomInfo == null) {
		return;
	}

	var renshu = roomInfo.conf.renshu;

	for (var i = 0; i < renshu; ++i) {
		var userId = roomInfo.seats[i].userId;
		if (userId > 0) {
			delete userLocation[userId];
			db.set_room_id_of_user(userId, null);
		}
	}

	delete rooms[roomId];
	totalRooms--;
	db.delete_room(roomId);
};

exports.getTotalRooms = function () {
	return totalRooms;
};

exports.getRoom = function (roomId) {
	return rooms[roomId];
};

exports.isCreator = function (roomId, userId) {
	var roomInfo = rooms[roomId];
	if (roomInfo == null) {
		return false;
	}
	return roomInfo.conf.creator == userId;
};

exports.enterRoom = function (roomId, userId, userName, callback) {
	var fnTakeSeat = function (room) {
		if (exports.getUserRoom(userId) == roomId) {
			//已存在
			return 0;
		}

		var roomInfo = rooms[roomId];
		var renshu = roomInfo.conf.renshu;

		for (var i = 0; i < renshu; ++i) {
			var seat = room.seats[i];
			if (seat.userId <= 0) {
				seat.userId = userId;
				seat.name = userName;
				userLocation[userId] = {
					roomId: roomId,
					seatIndex: i
				};
				//console.log(userLocation[userId]);
				db.update_seat_info(roomId, i, seat.userId, "", seat.name);
				//正常
				return 0;
			}
		}
		//房间已满
		return 1;
	};

	var room = rooms[roomId];
	if (room) {
		var ret = fnTakeSeat(room);
		callback(ret);
	}
	else {
		db.get_room_data(roomId, function (dbdata) {
			if (dbdata == null) {
				//找不到房间
				callback(2);
			}
			else {
				//construct room.
				room = constructRoomFromDb(dbdata);
				//
				var ret = fnTakeSeat(room);
				callback(ret);
			}
		});
	}
};

exports.setReady = function (userId, value) {
	var roomId = exports.getUserRoom(userId);
	if (roomId == null) {
		return;
	}

	var room = exports.getRoom(roomId);
	if (room == null) {
		return;
	}

	var seatIndex = exports.getUserSeat(userId);
	if (seatIndex == null) {
		return;
	}

	var s = room.seats[seatIndex];
	s.ready = value;
};

exports.isReady = function (userId) {
	var roomId = exports.getUserRoom(userId);
	if (roomId == null) {
		return;
	}

	var room = exports.getRoom(roomId);
	if (room == null) {
		return;
	}

	var seatIndex = exports.getUserSeat(userId);
	if (seatIndex == null) {
		return;
	}

	var s = room.seats[seatIndex];
	return s.ready;
};

exports.getUserRoom = function (userId) {
	var location = userLocation[userId];
	if (location != null) {
		return location.roomId;
	}
	return null;
};

exports.getUserSeat = function (userId) {
	var location = userLocation[userId];
	//console.log(userLocation[userId]);
	if (location != null) {
		return location.seatIndex;
	}
	return null;
};

exports.getUserLocations = function () {
	return userLocation;
};

exports.exitRoom = function (userId) {
	var location = userLocation[userId];
	if (location == null)
		return;

	var roomId = location.roomId;
	var seatIndex = location.seatIndex;
	var room = rooms[roomId];
	delete userLocation[userId];
	if (room == null || seatIndex == null) {
		return;
	}

	var seat = room.seats[seatIndex];
	seat.userId = 0;
	seat.name = "";

	var numOfPlayers = 0;
	for (var i = 0; i < room.seats.length; ++i) {
		if (room.seats[i].userId > 0) {
			numOfPlayers++;
		}
	}

	db.set_room_id_of_user(userId, null);

	if (numOfPlayers == 0) {
		exports.destroy(roomId);
	}
};